package org.nobject.common.db;

import java.util.HashMap;
import java.util.Map;

import org.nobject.common.lang.MapUtils;

public class SqlParser {

	public static Map parse(String sql,Map avlClassMap){
		String sql_up=sql.toUpperCase();
		int idx_selectWord	=sql_up.indexOf("SELECT");
		int idx_selectFrom	=sql_up.indexOf("FROM");
		int idx_whereWord	=sql_up.indexOf("WHERE");
		
		String sql_select=sql.substring(idx_selectWord+6, idx_selectFrom).trim().replaceAll("\\s*,\\s*", ",");
		String sql_from=idx_whereWord>0?sql.substring(idx_selectFrom+4, idx_whereWord):sql.substring(idx_selectFrom+4).trim().replaceAll("\\s*,\\s*", ",");
		
//		String[] sql_select_eles	=sql_select.split(",");
//		String[] sql_from_eles		=sql_from.split(",");
		
		
		Map m=MapUtils.toMap(new Object[][]{
			{"sql_select"	, sql_select},
			{"sql_from"		, sql_from},
		});
		
		
		
		return m;
		
	}
	
	public static void test(){
//		Map m=MapUtils.toMap(new Object[][]{
//		{"BasUser",ListUtils.toList(new String[]{
//			"userId","userName","userEmail","userFullname","userLockerstatus"
//		})},
//		{"BasRole",ListUtils.toList(new String[]{
//			"roleId","roleName","roleDesc","roleType"
//		})},
//		{"BasModu",ListUtils.toList(new String[]{
//			"moduId","moduName","moduCode","moduUrl"
//		})},
//		{"BasMenu",ListUtils.toList(new String[]{
//			"menuId","menuName","menuParam","menuParamable","moduId"
//		})}
//			
//	});
	
	
	
	
	String sql="SELECT *," +
			"t0.* , " +
			"t0.userId , " +
			"t2.roleId      ," +
			"count(1) AS userCount " +
			"FROM " +
			"BasUser t0," +
			"BasRole t2," +
			"(SELECT * FROM BasModu) t3," +
			"(SELECT * FROM BasMenu) AS t4 " +
			"LEFT JOIN (SELECT * FROM BasConfig) t5 ON t4.roleId=t5.roleId" +
			"  WHERE t0.roleId=t2.roleId";
	
	System.out.println(SqlParser.parse(sql,new HashMap()));;
	}
}
